package service;

import dao.RentDaoMapper;
import entity.Rent;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtil;

import java.util.ArrayList;
import java.util.List;

public class RentService {
    /**
     * 分页查询数据
     */
    public List<Rent> list(int pageIndex, int pageSize){
        SqlSession sqlSession=null;
        List<Rent> list=new ArrayList<Rent>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(RentDaoMapper.class).list(pageIndex,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }
    /**
     * 添加数据
     */
    public int add(Rent rent){
        int result = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(RentDaoMapper.class).add(rent);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    /**
     * 修改方法
     * @param id
     * @param status
     * @return
     */
    public int update(Integer id,Integer status) {
        SqlSession sqlSession = null;
        int result = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(RentDaoMapper.class).update(id, status);
            //提交事物
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    public int delete(int id){
        int result = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(RentDaoMapper.class).delete(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    /**
     * 根据carNum模糊查询数据
     * @param carNum
     * @return
     */
    public List<Rent> select(String carNum){
        List<Rent> list=new ArrayList<Rent>();
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(RentDaoMapper.class).select(carNum);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    /**
     * 根据carNum查询记录数，不重复
     * @param carNum
     * @return
     */
    public int count(String carNum){
        int result = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(RentDaoMapper.class).count(carNum);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }
}
